package com.mobimagic.lockscreen.view;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;

public class LinearGradientDrawable extends Drawable {

    Paint paint = new Paint();
    LinearGradient lg;
    private int[] colors = new int[] { Color.parseColor("#97aade"), Color.parseColor("#d1d5fc"),
            Color.parseColor("#d1d5fc") };
    private float[] positions;

    public void setColors(int[] colors, float[] positions) {
        this.colors = colors;
        this.positions = positions;
        setSharder();
        invalidateSelf();
    }

    @Override
    public void setBounds(int left, int top, int right, int bottom) {
        super.setBounds(left, top, right, bottom);
        setSharder();
    }

    private void setSharder() {
        Rect r = getBounds();
        int beginX = r.left + r.width() / 2;
        lg = new LinearGradient(beginX, r.top, beginX, r.bottom, colors, positions, Shader.TileMode.CLAMP); //
        paint.setShader(lg);
    }

    public LinearGradientDrawable() {

    }

    @Override
    public void draw(Canvas canvas) {
        Rect rect = getBounds();
        canvas.drawRect(rect, paint);
    }

    @Override
    public void setAlpha(int alpha) {
        paint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(ColorFilter cf) {

    }

    @Override
    public int getOpacity() {
        return PixelFormat.OPAQUE;
    }

}
